package base.stages
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import utils.DisplayUtil;
	
	/**
	 *舞台层 
	 * @author devin
	 * 
	 */	
	public class StageLayer
	{
		/**
		 * 最底背景层
		 */		
		private var _bottomLayer:StageLayerSprite;
		
		/**
		 * 基本游戏视图层
		 */		
		private var _baseLayer:StageLayerSprite;
		
		/**
		 * 战斗视图层(每次战斗前都会清空层中的对象)
		 */		
		private var _fightLayer:StageLayerSprite;
		
		/**
		 * 支持场景人物点击可移动层
		 */		
		private var _sceneMoveLayer:StageLayerSprite;
		
		/**
		 * 功能视图层
		 */		
		private var _functionLayer:StageLayerSprite;
		
		/**
		 * 动态视图层
		 */		
		private var _dynamicLayer:StageLayerSprite;
		
		/**
		 * 顶层视图层
		 */		
		private var _topLayer:StageLayerSprite;
		
		/**
		 *蒙板层用 
		 */		
		private var _maskShape:Sprite;
		
		/**
		 *最底背景层 
		 */		
		public static const BOTTOM_LAYER:int = 1;
		
		/**
		 *基本游戏视图层 
		 */		
		public static const BASE_LAYER:int = 2;
		
		/**
		 *战斗视图层(每次战斗前都会清空层中的对象)
		 */
		public static const FIGHT_LAYER:int = 3;
		
		/**
		 *支持场景人物点击可移动层 
		 */		
		public static const SCENE_MOVE_LAYER:int = 4;
		
		/**
		 *功能视图层
		 */
		public static const FUNCTION_LAYER:int = 5;
		
		/**
		 *动态视图层 
		 */		
		public static const DYNAMIC_LAYER:int = 6;		
		
		/**
		 *顶层视图层 
		 */		
		public static const TOP_LAYER:int = 7;
		
		/**
		 *通明蒙板 
		 */
		public static const ALPHA_MASK:int = 1;
		
		/**
		 *半通明灰色蒙板 
		 */
		public static const GRAY_MASK:int = 2;
		
		/**
		 *黑色蒙板 
		 */
		public static const BLACK_MASK:int = 3;
		
		private var _maskDic:Dictionary=new Dictionary(true);
		
		/**
		 *舞台层 
		 * 
		 */		
		public function StageLayer()
		{
		}
		
		private static var _instance:StageLayer;
		public static function get Instance():StageLayer
		{
			if(_instance == null)
			{
				_instance = new StageLayer();
			}
			return _instance;
		}
		
		/**
		 * 舞台层初始注册
		 * @param stage
		 * 
		 */		
		public function register(stage:Stage):void
		{
			//最底背景层创建并加载至舞台
			_bottomLayer=new StageLayerSprite();
			stage.addChild(_bottomLayer);
			
			//基本游戏视图层创建并加载至舞台
			_baseLayer=new StageLayerSprite();
			stage.addChild(_baseLayer);
			
			//战斗视图层(每次战斗前都会清空层中的对象)创建并加载至舞台
			_fightLayer=new StageLayerSprite();
			stage.addChild(_fightLayer);
			
			//支持场景人物点击可移动层
			_sceneMoveLayer=new StageLayerSprite();
			stage.addChild(_sceneMoveLayer);
			
			//功能视图层
			_functionLayer=new StageLayerSprite();
			stage.addChild(_functionLayer);
			
			//基本游戏视图层创建并加载至舞台
			_dynamicLayer=new StageLayerSprite();
			stage.addChild(_dynamicLayer);			
			
			//顶层视图层创建并加载至舞台
			_topLayer=new StageLayerSprite();
			stage.addChild(_topLayer);
		}
		
		/**
		 * 根据舞台层类型取得层
		 * @param type 层类型，见StageLayer.xx，分为：BASE_LAYER
		 * @return 
		 * 
		 */		
		public function getLayerByType(type:int):StageLayerSprite
		{
			switch(type)
			{
				case BASE_LAYER:
					return _baseLayer;
					break;
				case FIGHT_LAYER:
					return _fightLayer;
					break;
				case SCENE_MOVE_LAYER:
					return _sceneMoveLayer;
					break;
				case FUNCTION_LAYER:
					return _functionLayer;
					break;
				case BOTTOM_LAYER:
					return _bottomLayer;
					break;
				case DYNAMIC_LAYER:
					return _dynamicLayer;
					break;
				case TOP_LAYER:
					return _topLayer;
					break;
			}
			return null;
		}
		
		/**
		 * 增加显示对象至舞台状态层，在切换场景状态时，将强制清除DYNAMIC_LAYER、TOP_LAYER层类型的内容
		 * @param source 显示对象源
		 * @param type 要加到的层类型
		 * @param center 是否居中显示
		 * @param maskType 蒙板类型,0=无蒙板
		 * 
		 */		
		public function addLayer(source:DisplayObject, type:int, center:Boolean = false, maskType:int=0):void
		{
			var container:StageLayerSprite = getLayerByType(type);
			if(center)
			{
				var rect:Rectangle = DisplayUtil.getVisibleRect(source);
				source.x = (StageReflection.stageWidth - rect.width)/2;
				source.y = (StageReflection.stageHeight - rect.height)/2;
			}
			if(maskType!=0)
			{
				source.addEventListener(Event.REMOVED_FROM_STAGE, __onLayerSourceRemoved);
				var slm:StageLayerMask = new StageLayerMask(maskType);
				_maskDic[source.name]=slm;
				container.addLayer(slm);
			}
			if(type == DYNAMIC_LAYER)
			{
				source.addEventListener(MouseEvent.MOUSE_DOWN,__onSorceMouseDowm);
				_dynamicLayerList.push(source);
			}
			container.addLayer(source);
		}
		
		private var _dynamicLayerList:Vector.<DisplayObject> = new Vector.<DisplayObject>;
		/**
		 *动态层 支持拖动事件响应 
		 */		
		private function __onSorceMouseDowm(evt:MouseEvent):void
		{
			var point:Point = new Point(evt.localX,evt.localY);
			evt.stopImmediatePropagation();
			var sprite:Sprite = evt.target as Sprite;
			if(!sprite) return;
			if(_dynamicLayerList.indexOf(sprite) == -1) return;
			if(sprite.parent){
				sprite.parent.setChildIndex(sprite,sprite.parent.numChildren-1);
				trace("1111111111")
			}
			var rct:Rectangle = new Rectangle(0,0,sprite.width-25,25);
			if(!rct.containsPoint(point)) return;
			sprite.startDrag();
			sprite.addEventListener(MouseEvent.MOUSE_UP,__onSourceMouseUp)
		}
		private function __onSourceMouseUp(evt:MouseEvent):void
		{
			var sprite:Sprite = evt.target as Sprite;
			if(!sprite) return;
			if(_dynamicLayerList.indexOf(sprite) == -1) return;
			sprite.removeEventListener(MouseEvent.MOUSE_UP,__onSourceMouseUp);
			sprite.stopDrag();
		}
		private function __onLayerSourceRemoved(evt:Event):void
		{
			var source:DisplayObject=evt.target as DisplayObject;
			source.removeEventListener(Event.REMOVED_FROM_STAGE, __onLayerSourceRemoved);
			var sp:StageLayerMask=_maskDic[source.name] as StageLayerMask;
			if(sp)
			{
				if(sp.parent)sp.parent.removeChild(sp);
				sp.dispose();
			}
			sp=null;
			delete _maskDic[source.name];
			source=null;
		}
		
//		public function clearnStageDynamic():void
//		{
//			cleanSprite(_stageDynamicLayer);
//		}
//		
//		public function clearnGameDynamic():void
//		{
//			cleanSprite(_gameDynamicLayer);
//		}
		
		private function clearSprite(target:Sprite):void
		{
			while(target.numChildren > 0)
			{
				var child:DisplayObject = target.getChildAt(0);
				if(child.parent)child.parent.removeChild(child);
				child=null;
			}
		}
		
		/**
		 *根据舞台类型清除层 
		 * */
		public function clearLayer(source:DisplayObject,type:int):void
		{
			var container:StageLayerSprite = getLayerByType(type);
			if(container.contains(source)) container.removeLayer(source);
			if(type == DYNAMIC_LAYER) {
				source.removeEventListener(MouseEvent.MOUSE_DOWN,__onSorceMouseDowm);
				if(_dynamicLayerList.indexOf(source)>-1)_dynamicLayerList.splice(_dynamicLayerList.indexOf(source),1);
			}
			source=null;  
		}
		
		/**
		 *当场景状态发生改变时，清除的层内容(清除DYNAMIC_LAYER、TOP_LAYER)
		 * 
		 */		
		public function clearStateChangeLayer():void
		{
			while(_dynamicLayer.numChildren)
			{
				var source:DisplayObject= _dynamicLayer.getChildAt(0);
				_dynamicLayer.removeChild(source)
				DisplayUtil.dispose(source);
				source=null;
			}
			
			while(_topLayer.numChildren)
			{
				source= _topLayer.getChildAt(0);
				_topLayer.removeChild(source)
				DisplayUtil.dispose(source);
				source=null;
			}
		}
		
		/**
		 *设置对象层级索引 
		 * @param source 显示对象
		 * @param index 层级索引
		 * 
		 */		
		public function setSourceIndex(source:DisplayObject, index:int):void
		{
			if(!source)return;
			var sprite:DisplayObjectContainer=source.parent;
			sprite.setChildIndex(source, index);
		}
	}
}